//
//  CalculateLoanChartsView.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/18.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "CalculateLoanChartsView2.h"
#import "Masonry.h"
@import Charts;

@interface CalculateLoanChartsView2()
@property (nonatomic, strong)BarChartView *chartView;
@end

@implementation CalculateLoanChartsView2

#pragma mark Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadCalculateLoanChartsView];
        self.backgroundColor = BackgroundColor240;
    }
    return self;
}


#pragma mark Private
- (void)loadCalculateLoanChartsView{
    [self.chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
}

#pragma mark Public
- (void)setChartViewMoveToXPointAtIndex:(NSInteger)index{
    
    if (index >= 1&&index<_loanMoneyArray.count) {
        
        [self.chartView moveViewToX:(index-1)];
    }
}

#pragma mark Getter
- (BarChartView *)chartView{

    if (_chartView == nil) {
        _chartView = [[BarChartView alloc] init];
        _chartView.descriptionText = @"";
        _chartView.noDataTextDescription = @"You need to provide data for the chart.";
        
        _chartView.drawGridBackgroundEnabled = NO;
        
        _chartView.dragEnabled = YES;
        [_chartView setScaleEnabled:YES];
        _chartView.pinchZoomEnabled = NO;
        
        ChartYAxis *leftAxis = _chartView.leftAxis;
        leftAxis.startAtZeroEnabled = NO;
        
        ChartXAxis *xAxis = _chartView.xAxis;
        xAxis.labelPosition = XAxisLabelPositionBottom;
        
        _chartView.rightAxis.enabled = NO;
        
        _chartView.drawBarShadowEnabled = NO;
        _chartView.drawValueAboveBarEnabled = YES;
        
        _chartView.maxVisibleValueCount = 60;
        
        xAxis.labelPosition = XAxisLabelPositionBottom;
        xAxis.labelFont = [UIFont systemFontOfSize:10.f];
        xAxis.drawGridLinesEnabled = NO;
        xAxis.spaceBetweenLabels = 2.0;
        
        leftAxis.startAtZeroEnabled = YES;
        leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
        leftAxis.labelCount = 10;
        leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
        leftAxis.valueFormatter.maximumFractionDigits = 1;
        leftAxis.valueFormatter.negativeSuffix = @" 元";
        leftAxis.valueFormatter.positiveSuffix = @" 元";
        leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
        leftAxis.spaceTop = 0.15;
        
        _chartView.legend.position = ChartLegendPositionBelowChartLeft;
        _chartView.legend.form = ChartLegendFormSquare;
        _chartView.legend.formSize = 9.0;
        _chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
        _chartView.legend.xEntrySpace = 4.0;
        
        [self addSubview:_chartView];
 
    }
    
    return _chartView;
}

#pragma mark Setter
- (void)setLoanMoneyArray:(NSMutableArray *)loanMoneyArray{
    _loanMoneyArray = loanMoneyArray;
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < loanMoneyArray.count; i++)
    {
        [xVals addObject:[@(i+1) stringValue]];
    }
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    double maxVal = 0.0;
    double minVal = [loanMoneyArray[0] doubleValue];
    for (int i = 0; i < loanMoneyArray.count; i++)
    {
        double val = [loanMoneyArray[i] doubleValue];
        maxVal = MAX(maxVal, val);
        minVal = MIN(minVal, val);
        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:(i)]];
    }
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    [leftAxis removeAllLimitLines];
    leftAxis.customAxisMax = maxVal+100;
    leftAxis.customAxisMin = minVal-100;
    
    NSString *barChartText = @"月还款";
    if (_title!=nil) {
        barChartText = _title;
    }
    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:barChartText];
    set1.barSpace = 0.2;
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    
    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
    
    self.chartView.data = data;
    [self.chartView setVisibleXRangeMaximum:6];
}


@end
